package com.airnewslinkosky.ticket.sql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.BaseColumns;
import android.util.Log;

import com.airnewslinkosky.ticket.model.Contactor;
import com.airnewslinkosky.ticket.model.ItineraryInfo;
import com.airnewslinkosky.ticket.model.MileagerInfo;
import com.airnewslinkosky.ticket.model.Passenger;
import com.airnewslinkosky.ticket.util.FlyUtil;

public class FavoriteInfoDB {

	/**
	 * 添加乘机人常用信息
	 * @param context
	 * @param values
	 * @return
	 */
	public static long insertPsgInfo(Context context, ContentValues values) {
		DBHelper mOpenHelper = new DBHelper(context);
		long id = mOpenHelper.getWritableDatabase().insert(DBHelper.PASSENGER_TABLE_NAME,
				BaseColumns._ID, values);
		mOpenHelper.getWritableDatabase().close();
		return id;
	}
	
	/**
	 * 删除乘机人常用信息
	 * @param context
	 * @param id
	 */
	public static void deletePsgInfo(Context context, int id) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { String.valueOf(id) };
		mOpenHelper.getWritableDatabase().delete(DBHelper.PASSENGER_TABLE_NAME, BaseColumns._ID+"=?",
				param);
		mOpenHelper.getWritableDatabase().close();
	}
	
	/**
	 * 删除乘机人常用信息
	 * @param context
	 * @param id
	 */
	public static void deletePsgInfo(Context context, String pname,String pno) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { pname,pno};
		mOpenHelper.getWritableDatabase().delete(DBHelper.PASSENGER_TABLE_NAME, "passengerName=? and idNo=?",
				param);
		mOpenHelper.getWritableDatabase().close();
	}
	
	/**
	 * 删除全部乘机人常用信息
	 * @param context
	 */
	public static void deleteAllPsgInfo(Context context) {
		DBHelper mOpenHelper = new DBHelper(context);
		mOpenHelper.getWritableDatabase().delete(DBHelper.PASSENGER_TABLE_NAME, "1=1",
				null);
		mOpenHelper.getWritableDatabase().close();
	}
	
	/**
	 * 更新乘机人常用信息
	 * @param context
	 * @param values
	 * @return
	 */
	public static int updatePsgInfo(Context context, ContentValues values) {
		DBHelper mOpenHelper = new DBHelper(context);
		int id = mOpenHelper.getWritableDatabase().update(DBHelper.PASSENGER_TABLE_NAME, 
				values, "passengerName=? and idNo=?", new String[]{String.valueOf(values.get("passengerName")),String.valueOf(values.get("idNo"))});
		mOpenHelper.getWritableDatabase().close();
		return id;
	}

	
	/**
	 * 查询乘机人常用信息
	 * @param context
	 * @return
	 */
	public static Cursor selectPsgInfo(Context context) {
			DBHelper mOpenHelper = new DBHelper(context);
			Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.PASSENGER_TABLE_NAME,
					null, null, null, null, null, null);
			return c;
	}	

	public static List<Map<String, String>> selectPsgInfoList(Context context) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.PASSENGER_TABLE_NAME,
				null, null, null, null, null, BaseColumns._ID + " desc");
		c.moveToFirst();
		for (int i = 0; i < c.getCount(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("passengerName", c.getString(c.getColumnIndex("passengerName")));
			map.put("passengerIDNo", c.getString(c.getColumnIndex("idNo")));
			map.put("passengerIDType", c.getString(c.getColumnIndex("idType")));
			map.put("passengerMobile", c.getString(c.getColumnIndex("passengerMobile")));
			map.put("email", c.getString(c.getColumnIndex("email")));
			map.put("passagerBirthday", c.getString(c.getColumnIndex("birthday")));
			map.put("passengerIdNumber", FlyUtil.getCardTypeLabel(c.getString(c.getColumnIndex("idType"))) + " " + c.getString(c.getColumnIndex("idNo")));
			map.put("sex", c.getString(c.getColumnIndex("sex")));
			map.put("setDefault", c.getString(c.getColumnIndex("setDefault")));
			data.add(map);
			c.moveToNext();
		}
		c.close();
		mOpenHelper.getReadableDatabase().close();
		return data;
	}
	
	public static List<Map<String, String>> selectTopOnePsgInfoList(Context context) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		data = selectDefaultPsgInfoList(context);
		System.out.println("data="+data.size());
		if(data.size() == 0){
			DBHelper mOpenHelper = new DBHelper(context);
			Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.PASSENGER_TABLE_NAME,
					null, null, null, null, null, BaseColumns._ID + " desc");
			c.moveToFirst();
			for (int i = 0; i < c.getCount(); i++) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("passengerName", c.getString(c.getColumnIndex("passengerName")));
				map.put("passengerIDNo", c.getString(c.getColumnIndex("idNo")));
				map.put("passengerIDType", c.getString(c.getColumnIndex("idType")));
				map.put("passengerMobile", c.getString(c.getColumnIndex("passengerMobile")));
				map.put("email", c.getString(c.getColumnIndex("email")));
				map.put("passagerBirthday", c.getString(c.getColumnIndex("birthday")));
				map.put("passengerIdNumber", FlyUtil.getCardTypeLabel(c.getString(c.getColumnIndex("idType"))) + " " + c.getString(c.getColumnIndex("idNo")));
				map.put("sex", c.getString(c.getColumnIndex("sex")));
				map.put("setDefault", c.getString(c.getColumnIndex("setDefault")));
				data.add(map);
				break;
			}
			c.close();
			mOpenHelper.getReadableDatabase().close();
		}
		return data;
	}
	
	public static   List<Map<String, String>> selectDefaultPsgInfoList(Context context) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.PASSENGER_TABLE_NAME,
				null, "setDefault == '1'",  null, null, null, BaseColumns._ID + " desc");
		c.moveToFirst();
		for (int i = 0; i < c.getCount(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("passengerName", c.getString(c.getColumnIndex("passengerName")));
			map.put("passengerIDNo", c.getString(c.getColumnIndex("idNo")));
			map.put("passengerIDType", c.getString(c.getColumnIndex("idType")));
			map.put("passengerMobile", c.getString(c.getColumnIndex("passengerMobile")));
			map.put("email", c.getString(c.getColumnIndex("email")));
			map.put("passagerBirthday", c.getString(c.getColumnIndex("birthday")));
			map.put("passengerIdNumber", FlyUtil.getCardTypeLabel(c.getString(c.getColumnIndex("idType"))) + " " + c.getString(c.getColumnIndex("idNo")));
			map.put("sex", c.getString(c.getColumnIndex("sex")));
			map.put("setDefault", c.getString(c.getColumnIndex("setDefault")));
			data.add(map);
			c.moveToNext();
		}
		c.close();
		mOpenHelper.getReadableDatabase().close();
		return data;
	}
	//取登机人列表
	public static ArrayList<Passenger> getPassengerList(Context context){
		ArrayList<Passenger> list = new ArrayList<Passenger>();
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor cur = mOpenHelper.getReadableDatabase().query(DBHelper.PASSENGER_TABLE_NAME,
				null, null, null, null, null, null);
		for(int i=0,count = cur.getCount(); i < count; i++){
			if (i >= 10) {
				break;
			}
			cur.moveToPosition(i);
			Passenger passenger = new Passenger();
			passenger.setPassengerName(cur.getString(cur.getColumnIndex("passengerName")));
			passenger.setPassagerType(cur.getString(cur.getColumnIndex("passengerType")));
			passenger.setPassengerIDType(cur.getString(cur.getColumnIndex("idType")));
			passenger.setPassengerIDNo(cur.getString(cur.getColumnIndex("idNo")));
			passenger.setPassagerBirthday(cur.getString(cur.getColumnIndex("birthday")));
			passenger.setPassengerMobile(cur.getString(cur.getColumnIndex("passengerMobile")));
			passenger.setSex(cur.getString(cur.getColumnIndex("sex")));
			list.add(passenger);
		}
		cur.close();
		mOpenHelper.getReadableDatabase().close();
		return list;
	}
	
	//保存登机人
	public static void setPassenger(Context context, Passenger passenger){
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor cur = mOpenHelper.getReadableDatabase().query(DBHelper.PASSENGER_TABLE_NAME,
				null, null, null, null, null, null);
		for(int i=0,count = cur.getCount(); i < count; i++ ){
			cur.moveToPosition(i);
			String name = cur.getString(cur.getColumnIndex("passengerName"));
			String idNo = cur.getString(cur.getColumnIndex("idNo"));
			if(idNo.equals(passenger.getPassengerIDNo())){
				Log.i("FavoriteInfoDB.deletePsgInfo",  name);
				FavoriteInfoDB.deletePsgInfo(context, cur.getInt(cur.getColumnIndex(BaseColumns._ID)));
			}
		}
		ContentValues values = new ContentValues();
		values.put("passengerName",passenger.getPassengerName());
		values.put("passengerType", passenger.getPassagerType());
		values.put("idType", passenger.getPassengerIDType());
		values.put("idNo", passenger.getPassengerIDNo());
		values.put("birthday",  passenger.getPassagerBirthday());
		values.put("passengerMobile", passenger.getPassengerMobile());
		values.put("email", passenger.getEmail());
		values.put("sex", passenger.getSex());
		values.put("setDefault", "0");
		cur.close();
		mOpenHelper.getReadableDatabase().close();
		FavoriteInfoDB.insertPsgInfo(context, values);
	}
	
	
	/**
	 * 添加联系人常用信息
	 * @param context
	 * @param values
	 * @return
	 */
	public static long insertContactorInfo(Context context, ContentValues values) {
		DBHelper mOpenHelper = new DBHelper(context);
		long id = mOpenHelper.getWritableDatabase().insert(DBHelper.CONTACTOR_TABLE_NAME,
				BaseColumns._ID, values);
		mOpenHelper.getWritableDatabase().close();
		return id;
	}
	
	/**
	 * 删除联系人常用信息
	 * @param context
	 * @param id
	 */
	public static void deleteContactorInfo(Context context, int id) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { String.valueOf(id) };
		mOpenHelper.getWritableDatabase().delete(DBHelper.CONTACTOR_TABLE_NAME, BaseColumns._ID+"=?",
				param);
		mOpenHelper.getWritableDatabase().close();
	}

	/**
	 * 删除联系人常用信息
	 * @param context
	 * @param id
	 */
	public static void deleteContactorInfo(Context context, String name) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { name};
		mOpenHelper.getWritableDatabase().delete(DBHelper.CONTACTOR_TABLE_NAME, "contactName=?",
				param);
		mOpenHelper.getWritableDatabase().close();
	}
	
	/**
	 * 删除全部联系人常用信息
	 * @param context
	 * @param id
	 */
	public static void deleteAllContactorInfo(Context context) {
		DBHelper mOpenHelper = new DBHelper(context);
		mOpenHelper.getWritableDatabase().delete(DBHelper.CONTACTOR_TABLE_NAME, null,
				null);
		mOpenHelper.getWritableDatabase().close();
	}

	/**
	 * 更新所有联系人信息的默认状态为0（无设置）
	 * @param context
	 * @param values
	 * @return
	 */
	public static int updateContactorInfoClearDefault(Context context) {
		DBHelper mOpenHelper = new DBHelper(context);
		ContentValues values = new ContentValues();
		values.put("setDefault", "0");
		int ret = mOpenHelper.getWritableDatabase().update(DBHelper.CONTACTOR_TABLE_NAME, 
				values, null, null );
		mOpenHelper.getWritableDatabase().close();
		return ret;
	}
	
	/**
	 * 更新联系人信息
	 * @param context
	 * @param values
	 * @return
	 */
	public static int updateContactorInfo(Context context,  int id, ContentValues values) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { String.valueOf(id) };
		int ret = mOpenHelper.getWritableDatabase().update(DBHelper.CONTACTOR_TABLE_NAME, 
				values, BaseColumns._ID+"=?", param );
		mOpenHelper.getWritableDatabase().close();
		return ret;
	}
	
	//保存联系人
	public static void setContactor(Context context, Contactor contactor){
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor cur = mOpenHelper.getReadableDatabase().query(DBHelper.CONTACTOR_TABLE_NAME,
				null, null, null, null, null, null);
//		if(cur.getCount()>0){
//			deleteAllContactorInfo(context);
//		}
		for(int i=0,count = cur.getCount(); i < count; i++ ){
			cur.moveToPosition(i);
			String name = cur.getString(cur.getColumnIndex("contactName"));
			if(name.equals(contactor.getContactName())){
				FavoriteInfoDB.deleteContactorInfo(context, cur.getInt(cur.getColumnIndex(BaseColumns._ID)));
			}
		}
		
		ContentValues values = new ContentValues();
		values.put("contactName",contactor.getContactName());
		values.put("contactNO", contactor.getContactNO());
		values.put("phone", contactor.getPhone());
		values.put("email",  contactor.getEmail());
		values.put("sex",  contactor.getSex());
		values.put("birthday",  contactor.getBirthday());
		values.put("setDefault", "0");
		cur.close();
		mOpenHelper.getReadableDatabase().close();
		FavoriteInfoDB.insertContactorInfo(context, values);
	}
	
	//取联系人列表
	public static ArrayList<Contactor> getContactorList(Context context){
		ArrayList<Contactor> list = new ArrayList<Contactor>();
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor cur = mOpenHelper.getReadableDatabase().query(DBHelper.CONTACTOR_TABLE_NAME,
				null, null, null, null, null, null);
		for(int i=0,count = cur.getCount(); i < count; i++){
			if (i >= 10) {
				break;
			}
			cur.moveToPosition(i);
			Contactor contactor = new Contactor();
			contactor.setContactNO(cur.getString(cur.getColumnIndex("contactNO")));
			contactor.setContactName(cur.getString(cur.getColumnIndex("contactName")));
			contactor.setPhone(cur.getString(cur.getColumnIndex("phone")));
			contactor.setEmail(cur.getString(cur.getColumnIndex("email")));
			contactor.setSex(cur.getString(cur.getColumnIndex("sex")));
			contactor.setBirthday(cur.getString(cur.getColumnIndex("birthday")));
			list.add(contactor);
		}
		cur.close();
		mOpenHelper.getReadableDatabase().close();
		return list;
	}
	
	public static List<Map<String, String>> selectContactorInfoList(Context context) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.CONTACTOR_TABLE_NAME,
				null, null, null, null, null, BaseColumns._ID + " desc");
		c.moveToFirst();
		for (int i = 0; i < c.getCount(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			Log.i("selectContactorInfoList contactName",  c.getString(c.getColumnIndex("contactName")));
			Log.i("selectContactorInfoList phone",  c.getString(c.getColumnIndex("phone")));
			map.put("contactName", c.getString(c.getColumnIndex("contactName")));
			map.put("phone", c.getString(c.getColumnIndex("phone")));
			map.put("email", c.getString(c.getColumnIndex("email")));
			map.put("sex", c.getString(c.getColumnIndex("sex")));
			map.put("birthday", c.getString(c.getColumnIndex("birthday")));
			map.put("setDefault", c.getString(c.getColumnIndex("setDefault")));
			map.put(BaseColumns._ID, c.getString(c.getColumnIndex(BaseColumns._ID)));
			data.add(map);
			c.moveToNext();
		}
		c.close();
		mOpenHelper.getReadableDatabase().close();
		return data;
	}

	public static List<Map<String, String>> selectTopOneContactorInfoList(Context context) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		data = selectDefaultContactorInfoList(context);
		if(data.size() == 0){
			DBHelper mOpenHelper = new DBHelper(context);
			Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.CONTACTOR_TABLE_NAME,
					null, null, null, null, null, BaseColumns._ID + " desc");
			c.moveToFirst();
			for (int i = 0; i < c.getCount(); i++) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("contactName", c.getString(c.getColumnIndex("contactName")));
				map.put("phone", c.getString(c.getColumnIndex("phone")));
				map.put("email", c.getString(c.getColumnIndex("email")));
				map.put("sex", c.getString(c.getColumnIndex("sex")));
				map.put("birthday", c.getString(c.getColumnIndex("birthday")));
				map.put("setDefault", c.getString(c.getColumnIndex("setDefault")));
				map.put(BaseColumns._ID, c.getString(c.getColumnIndex(BaseColumns._ID)));
				data.add(map);
				break;
			}
			c.close();
			mOpenHelper.getReadableDatabase().close();
		}
		return data;
	}
	
	public static List<Map<String, String>> selectDefaultContactorInfoList(Context context) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.CONTACTOR_TABLE_NAME,
				null, "setDefault == '1'", null, null, null, BaseColumns._ID + " desc");
		c.moveToFirst();
		for (int i = 0; i < c.getCount(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("contactName", c.getString(c.getColumnIndex("contactName")));
			map.put("phone", c.getString(c.getColumnIndex("phone")));
			map.put("email", c.getString(c.getColumnIndex("email")));
			map.put("sex", c.getString(c.getColumnIndex("sex")));
			map.put("birthday", c.getString(c.getColumnIndex("birthday")));
			map.put("setDefault", c.getString(c.getColumnIndex("setDefault")));
			map.put(BaseColumns._ID, c.getString(c.getColumnIndex(BaseColumns._ID)));
			data.add(map);
			c.moveToNext();
		}
		c.close();
		mOpenHelper.getReadableDatabase().close();
		return data;
	}
	
	
	
	/**
	 * 添加收件人常用信息
	 * @param context
	 * @param values
	 * @return
	 */
	public static long insertItineraryInfo(Context context, ContentValues values) {
		DBHelper mOpenHelper = new DBHelper(context);
		long id = mOpenHelper.getWritableDatabase().insert(DBHelper.ITINERARY_TABLE_NAME,
				BaseColumns._ID, values);
		mOpenHelper.getWritableDatabase().close();
		return id;
	}
	
	/**
	 * 删除收件人常用信息
	 * @param context
	 * @param id
	 */
	public static void deleteItineraryInfo(Context context, int id) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { String.valueOf(id) };
		mOpenHelper.getWritableDatabase().delete(DBHelper.ITINERARY_TABLE_NAME, BaseColumns._ID+"=?",
				param);
		mOpenHelper.getWritableDatabase().close();
	}

	/**
	 * 删除收件人常用信息
	 * @param context
	 * @param id
	 */
	public static void deleteItineraryInfo(Context context, String name) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { name};
		mOpenHelper.getWritableDatabase().delete(DBHelper.ITINERARY_TABLE_NAME, "consigneeName=?",
				param);
		mOpenHelper.getWritableDatabase().close();
	}
	
	/**
	 * 删除全部收件人常用信息
	 * @param context
	 * @param id
	 */
	public static void deleteAllItineraryInfo(Context context) {
		DBHelper mOpenHelper = new DBHelper(context);
		mOpenHelper.getWritableDatabase().delete(DBHelper.ITINERARY_TABLE_NAME, null,
				null);
		mOpenHelper.getWritableDatabase().close();
	}
	
	/**
	 * 更新收件人信息
	 * @param context
	 * @param values
	 * @return
	 */
	public static int updateItineraryInfo(Context context,  int id, ContentValues values) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { String.valueOf(id) };
		int ret = mOpenHelper.getWritableDatabase().update(DBHelper.ITINERARY_TABLE_NAME, 
				values, BaseColumns._ID+"=?", param );
		mOpenHelper.getWritableDatabase().close();
		return ret;
	}
	
	/**
	 * 更新所有信息的默认状态为0（无设置）
	 * @param context
	 * @param values
	 * @return
	 */
	public static int updateItineraryInfoClearDefault(Context context) {
		DBHelper mOpenHelper = new DBHelper(context);
		ContentValues values = new ContentValues();
		values.put("setDefault", "0");
		int ret = mOpenHelper.getWritableDatabase().update(DBHelper.ITINERARY_TABLE_NAME, 
				values, null, null );
		mOpenHelper.getWritableDatabase().close();
		return ret;
	}
	
	//保存收件人
	public static void setItineraryInfo(Context context, ItineraryInfo itineraryInfo){
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor cur = mOpenHelper.getReadableDatabase().query(DBHelper.ITINERARY_TABLE_NAME,
				null, null, null, null, null, null);
		for(int i=0,count = cur.getCount(); i < count; i++ ){
			cur.moveToPosition(i);
			String name = cur.getString(cur.getColumnIndex("consigneeName"));
			if(name.equals(itineraryInfo.getConsigneeName())){
				FavoriteInfoDB.deleteItineraryInfo(context, cur.getInt(cur.getColumnIndex(BaseColumns._ID)));
			}
		}
		ContentValues values = new ContentValues();
		values.put("consigneeName",itineraryInfo.getConsigneeName());
		values.put("consigneePhone", itineraryInfo.getConsigneePhone());
		values.put("consigneeAddress", itineraryInfo.getConsigneeAddress());
		values.put("consigneeZIP",  itineraryInfo.getConsigneeZIP());
		values.put("setDefault", "0");
		cur.close();
		mOpenHelper.getReadableDatabase().close();
		FavoriteInfoDB.insertItineraryInfo(context, values);
	}
	
	//取收件人列表
	public static ArrayList<ItineraryInfo> getItineraryInfoList(Context context){
		ArrayList<ItineraryInfo> list = new ArrayList<ItineraryInfo>();
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor cur = mOpenHelper.getReadableDatabase().query(DBHelper.ITINERARY_TABLE_NAME,
				null, null, null, null, null, null);
		for(int i=0,count = cur.getCount(); i < count; i++){
			if (i >= 10) {
				break;
			}
			cur.moveToPosition(i);
			ItineraryInfo itineraryInfo = new ItineraryInfo();
			itineraryInfo.setConsigneeName(cur.getString(cur.getColumnIndex("consigneeName")));
			itineraryInfo.setConsigneePhone(cur.getString(cur.getColumnIndex("consigneePhone")));
			itineraryInfo.setConsigneeAddress(cur.getString(cur.getColumnIndex("consigneeAddress")));
			itineraryInfo.setConsigneeZIP(cur.getString(cur.getColumnIndex("consigneeZIP")));
			list.add(itineraryInfo);
		}
		cur.close();
		mOpenHelper.getReadableDatabase().close();
		return list;
	}
	
	public static List<Map<String, String>> selectItineraryInfoList(Context context) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.ITINERARY_TABLE_NAME,
				null, null, null, null, null, BaseColumns._ID + " desc");
		c.moveToFirst();
		for (int i = 0; i < c.getCount(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("consigneeName", c.getString(c.getColumnIndex("consigneeName")));
			map.put("consigneePhone", c.getString(c.getColumnIndex("consigneePhone")));
			map.put("consigneeAddress", c.getString(c.getColumnIndex("consigneeAddress")));
			map.put("consigneeZIP", c.getString(c.getColumnIndex("consigneeZIP")));
			map.put("setDefault", c.getString(c.getColumnIndex("setDefault")));
			map.put(BaseColumns._ID, c.getString(c.getColumnIndex(BaseColumns._ID)));
			data.add(map);
			c.moveToNext();
		}
		c.close();
		mOpenHelper.getReadableDatabase().close();
		return data;
	}

	public static List<Map<String, String>> selectTopOneItineraryInfoList(Context context) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		data = selectDefaultItineraryInfoList(context);
		if(data.size() == 0){
			DBHelper mOpenHelper = new DBHelper(context);
			Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.ITINERARY_TABLE_NAME,
					null, null, null, null, null, BaseColumns._ID + " desc");
			c.moveToFirst();
			for (int i = 0; i < c.getCount(); i++) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("consigneeName", c.getString(c.getColumnIndex("consigneeName")));
				map.put("consigneePhone", c.getString(c.getColumnIndex("consigneePhone")));
				map.put("consigneeAddress", c.getString(c.getColumnIndex("consigneeAddress")));
				map.put("consigneeZIP", c.getString(c.getColumnIndex("consigneeZIP")));
				map.put("setDefault", c.getString(c.getColumnIndex("setDefault")));
				map.put(BaseColumns._ID, c.getString(c.getColumnIndex(BaseColumns._ID)));
				data.add(map);
				break;
			}
			c.close();
			mOpenHelper.getReadableDatabase().close();
		}
		return data;
	}
	
	public static List<Map<String, String>> selectDefaultItineraryInfoList(Context context) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.ITINERARY_TABLE_NAME,
				null, "setDefault == '1'", null, null, null, BaseColumns._ID + " desc");
		c.moveToFirst();
		for (int i = 0; i < c.getCount(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("consigneeName", c.getString(c.getColumnIndex("consigneeName")));
			map.put("consigneePhone", c.getString(c.getColumnIndex("consigneePhone")));
			map.put("consigneeAddress", c.getString(c.getColumnIndex("consigneeAddress")));
			map.put("consigneeZIP", c.getString(c.getColumnIndex("consigneeZIP")));
			map.put("setDefault", c.getString(c.getColumnIndex("setDefault")));
			map.put(BaseColumns._ID, c.getString(c.getColumnIndex(BaseColumns._ID)));
			data.add(map);
			c.moveToNext();
		}
		c.close();
		mOpenHelper.getReadableDatabase().close();
		return data;
	}
	
	/**
	 * 添加常旅客信息
	 * @param context
	 * @param values
	 * @return
	 */
	public static long insertMileagerInfo(Context context, ContentValues values) {
		DBHelper mOpenHelper = new DBHelper(context);
		long id = mOpenHelper.getWritableDatabase().insert(DBHelper.MILEAGER_TABLE_NAME,
				BaseColumns._ID, values);
		mOpenHelper.getWritableDatabase().close();
		return id;
	}
	
	/**
	 * 删除常旅客信息
	 * @param context
	 * @param id
	 */
	public static void deleteMileagerInfo(Context context, int id) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { String.valueOf(id) };
		mOpenHelper.getWritableDatabase().delete(DBHelper.MILEAGER_TABLE_NAME, BaseColumns._ID+"=?",
				param);
		mOpenHelper.getWritableDatabase().close();
	}

	/**
	 * 删除常旅客信息
	 * @param context
	 * @param id
	 */
	public static void deleteMileagerInfo(Context context, String mileagerCardAirline,String mileagerCardNumber) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { mileagerCardAirline,mileagerCardNumber};
		mOpenHelper.getWritableDatabase().delete(DBHelper.MILEAGER_TABLE_NAME, "mileagerCardAirline = ? and mileagerCardNumber = ?",
				param);
		mOpenHelper.getWritableDatabase().close();
	}
	
	/**
	 * 删除全部常旅客信息
	 * @param context
	 * @param id
	 */
	public static void deleteAllMileagerInfo(Context context) {
		DBHelper mOpenHelper = new DBHelper(context);
		mOpenHelper.getWritableDatabase().delete(DBHelper.MILEAGER_TABLE_NAME, null,
				null);
		mOpenHelper.getWritableDatabase().close();
	}
	
	/**
	 * 更新常旅客信息
	 * @param context
	 * @param values
	 * @return
	 */
	public static int updateMileagerInfo(Context context,  int id, ContentValues values) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { String.valueOf(id) };
		int ret = mOpenHelper.getWritableDatabase().update(DBHelper.MILEAGER_TABLE_NAME, 
				values, BaseColumns._ID+"=?", param );
		mOpenHelper.getWritableDatabase().close();
		return ret;
	}
	
	//保存收件人
	public static void setMileagerInfo(Context context, MileagerInfo mileagerInfo){
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor cur = mOpenHelper.getReadableDatabase().query(DBHelper.MILEAGER_TABLE_NAME,
				null, null, null, null, null, null);
		for(int i=0,count = cur.getCount(); i < count; i++ ){
			cur.moveToPosition(i);
			String mileagerCardAirline = cur.getString(cur.getColumnIndex("mileagerCardAirline"));
			String mileagerCardNumber = cur.getString(cur.getColumnIndex("mileagerCardNumber"));
			if(mileagerCardAirline.equals(mileagerInfo.getMileagerCardAirline()) && mileagerCardNumber.equals(mileagerInfo.getMileagerCardNumber())){
				FavoriteInfoDB.deleteMileagerInfo(context, mileagerCardAirline,mileagerCardNumber);
			}
		}
		ContentValues values = new ContentValues();
		values.put("mileagerCardAirline",mileagerInfo.getMileagerCardAirline());
		values.put("mileagerCardNumber", mileagerInfo.getMileagerCardNumber());
		values.put("mileagerInfo", mileagerInfo.getMileagerInfo());
		values.put("passengerNO",  mileagerInfo.getPassengerNO());
		values.put("mileagerNO",  mileagerInfo.getMileagerNO());
		cur.close();
		mOpenHelper.getReadableDatabase().close();
		FavoriteInfoDB.insertMileagerInfo(context, values);
	}
	
	//取常旅客列表
	public static ArrayList<MileagerInfo> getMileagerInfoList(Context context){
		ArrayList<MileagerInfo> list = new ArrayList<MileagerInfo>();
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor cur = mOpenHelper.getReadableDatabase().query(DBHelper.MILEAGER_TABLE_NAME,
				null, null, null, null, null, null);
		for(int i=0,count = cur.getCount(); i < count; i++){
			if (i >= 10) {
				break;
			}
			cur.moveToPosition(i);
			MileagerInfo mileagerInfo = new MileagerInfo();
			mileagerInfo.setMileagerCardAirline(cur.getString(cur.getColumnIndex("mileagerCardAirline")));
			mileagerInfo.setMileagerCardNumber(cur.getString(cur.getColumnIndex("mileagerCardNumber")));
			mileagerInfo.setMileagerInfo(cur.getString(cur.getColumnIndex("mileagerInfo")));
			mileagerInfo.setPassengerNO(cur.getString(cur.getColumnIndex("passengerNO")));
			mileagerInfo.setMileagerNO(cur.getString(cur.getColumnIndex("mileagerNO")));
			list.add(mileagerInfo);
		}
		cur.close();
		mOpenHelper.getReadableDatabase().close();
		return list;
	}
	
	public static List<Map<String, String>> selectMileagerInfoList(Context context) {
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		DBHelper mOpenHelper = new DBHelper(context);
		Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.MILEAGER_TABLE_NAME,
				null, null, null, null, null, null);
		c.moveToFirst();
		for (int i = 0; i < c.getCount(); i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("mileagerCardAirline", c.getString(c.getColumnIndex("mileagerCardAirline")));
			map.put("mileagerCardNumber", c.getString(c.getColumnIndex("mileagerCardNumber")));
			map.put("mileagerInfo", c.getString(c.getColumnIndex("mileagerInfo")));
			map.put("mileagerNO", c.getString(c.getColumnIndex("mileagerNO")));
			map.put("passengerNO", c.getString(c.getColumnIndex("passengerNO")));
			data.add(map);
			c.moveToNext();
		}
		c.close();
		mOpenHelper.getReadableDatabase().close();
		return data;
	}
	/**
	 * 添加常用航班信息
	 * @param context
	 * @param values
	 * @return
	 */
	public static long insertFlySetInfo(Context context, ContentValues values) {
		DBHelper mOpenHelper = new DBHelper(context);
		long id = mOpenHelper.getWritableDatabase().insert(DBHelper.SYSTEM_TABLE_NAME,
				BaseColumns._ID, values);
		mOpenHelper.getWritableDatabase().close();
		return id;
	}
	
	/**
	 * 删除常用航班
	 * @param context
	 * @param id
	 */
	public static void deleteFlySetInfo(Context context, int id) {
		DBHelper mOpenHelper = new DBHelper(context);
		String[] param = new String[] { String.valueOf(id) };
		mOpenHelper.getWritableDatabase().delete(DBHelper.SYSTEM_TABLE_NAME, BaseColumns._ID+"=?",
				param);
		mOpenHelper.getWritableDatabase().close();
	}
	
	
	/**
	 * 删除全部常用航班信息
	 * @param context
	 */
	public static void deleteAllFlySetInfo(Context context) {
		DBHelper mOpenHelper = new DBHelper(context);
		mOpenHelper.getWritableDatabase().delete(DBHelper.SYSTEM_TABLE_NAME, "1=1",
				null);
		mOpenHelper.getWritableDatabase().close();
	}
	
	/**
	 * 更新常用参数常用信息
	 * @param context
	 * @param values
	 * @return
	 */
	public static int updateFlySetInfo(Context context, ContentValues values) {
		DBHelper mOpenHelper = new DBHelper(context);
		int id = mOpenHelper.getWritableDatabase().update(DBHelper.SYSTEM_TABLE_NAME, 
				values, "systemType=?", new String[]{String.valueOf(values.get("systemType"))});
		mOpenHelper.getWritableDatabase().close();
		return id;
	}
	
	/**
	 * 查询常用参数常用信息
	 * @param context
	 * @return
	 */
	public static Cursor selectFlySetInfo(Context context,String systemType) {
			DBHelper mOpenHelper = new DBHelper(context);
			Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.SYSTEM_TABLE_NAME,
					new String[] {"systemType","systemName"}, "systemType=?", new String[]{systemType}, null, null, null);
			//mOpenHelper.getReadableDatabase().close();
			return c;
	}	
	
	/**
	 * 查询常用参数常用信息
	 * @param context
	 * @return
	 */
	public static String selectFlySetInfoValue(Context context,String systemType) {
		    String systemName = "";
			DBHelper mOpenHelper = new DBHelper(context);
			Cursor c = mOpenHelper.getReadableDatabase().query(DBHelper.SYSTEM_TABLE_NAME,
					new String[] {"systemType","systemName"}, "systemType=?", new String[]{systemType}, null, null, null);
			if (c.moveToFirst()) {
				systemName = c.getString(1);
			}
			c.close();
			mOpenHelper.getReadableDatabase().close();
			return systemName;
	}	
	
	
	/**
	 * 删除常用参数常用信息
	 * @param context
	 */
	public static void deleteFlySetInfoValue(Context context,String systemType) {
		DBHelper mOpenHelper = new DBHelper(context);
		mOpenHelper.getWritableDatabase().delete(DBHelper.SYSTEM_TABLE_NAME, 
				"systemType=?", new String[]{systemType});
		mOpenHelper.getWritableDatabase().close();
	}
}
